
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/hj_model/video_model.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_simple_cell_widget.dart';
import 'package:tiktokadult/hj_page/home_business/home_cell_style/video_type_menu_view.dart';
import 'package:tiktokadult/hj_page/home_business/search_page/search_result_page/search_video_result_page.dart';
import 'package:tiktokadult/hj_page/home_business/tag/video_tag_page.dart';
import 'package:tiktokadult/hj_utils/api_service/search_service.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/debug_log.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';

import '../../../../hj_model/home/video_list_resp.dart';
import '../../../../tools_base/refresh/pull_refresh.dart';
import '../widget/search_user_cell.dart';

class SearchUserResultPage extends StatefulWidget {
  final String keywords;
  final SearchType type;

  SearchUserResultPage({
    super.key,
    required this.type,
    required this.keywords,
  });

  @override
  State<SearchUserResultPage> createState() => _SearchUserResultPageState();
}

class _SearchUserResultPageState extends State<SearchUserResultPage> {
  RefreshController refreshController = RefreshController();
  int pageNumber = 1;
  List<VideoModel>? videoList;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _loadData();
    });
  }

  _loadData({int page = 1, int size = 12}) async {
    try {
      VideoListResp? retResp = await SearchService.getMediaSearchData(widget.keywords,
          pageNumber: page, pageSize: size, realm: widget.type.realm);
      pageNumber = page;
      videoList ??= [];
      videoList?.addAll(retResp?.videos ?? []);
      if (retResp?.hasNext == false) {
        refreshController.loadNoData();
      } else {
        refreshController.loadComplete();
      }
    } catch (e) {
      refreshController.loadComplete();
      debugLog(e);
    }
    refreshController.refreshCompleted();
    videoList ??= [];
    if (mounted) setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (videoList == null) {
      return LoadingCenterWidget();
    } else if (videoList?.isEmpty == true) {
      return CErrorWidget(
        retryOnTap: () {
          videoList = null;
          setState(() {});
          _loadData();
        },
      );
    } else {
      return pullYsRefresh(
        onInit: (ctr) => refreshController = ctr,
        onLoading: (ctr) => _loadData(page: pageNumber + 1),
        onRefresh: (ctr) => _loadData(),
        child: ListView.builder(
          padding: EdgeInsets.zero,
          itemCount: videoList?.length ?? 0,
          itemBuilder: (context, index) {
            return SearchUserCell();
          },
        ),
      );
    }
  }

}
